const WebSocket = require('ws');
const http = require('http');
 
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('WebSocket Server\n');
});
 
const wss = new WebSocket.Server({ server });
 
wss.on('connection', (ws) => {
  console.log('connect OK!');
 
  ws.on('message', (message) => {
    console.log('收到消息：', message);
 
    // 向客户端发送消息
    setInterval(() => {
       ws.send('/api/Interface/userInfo');
    }, 1000);
  });
 
  // 连接关闭时的回调
  ws.on('close', () => {
    console.log('客户端已断开连接');
  });
});
 
server.listen(3000, () => {
  console.log('WebSocket 服务器已启动在 http://localhost:3000');
});